<template>
  <el-dialog
    :title="!dataForm.id ? '新项目发布' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名" prop="projectName">
      <el-input v-model="dataForm.projectName" placeholder="项目名"></el-input>
    </el-form-item>
    <el-form-item label="项目描述" prop="projectDes" v-if="!dataForm.id">
      <el-input
        v-model="dataForm.projectDes"
        type="textarea"
        autosize
        placeholder="项目描述"
        clearable>
      </el-input>
    </el-form-item>
    <el-form-item label="所属学院" prop="collegeId" v-if="!dataForm.id">
      <el-tree
        v-model="dataForm.collegeId"
        :data="collegeList"
        :props="collegeListTreeProps"
        node-key="collegeId"
        ref="collegeListTree"
        :default-expand-all="true"
        @check-change="handleCheckChange"
        show-checkbox>
      </el-tree>
    </el-form-item>
    <el-form-item label="项目导师">
      <template>
        <el-select v-model="dataForm.tutorId" placeholder="请选择" >
          <el-option
            v-for="item in tutorList"
            :key="item.tutor_id"
            :label="item.username"
            :value="item.tutor_id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="开发团队" >
      <template>
        <el-select v-model="dataForm.teamId" placeholder="请选择">
          <el-option
            v-for="item in teamList"
            :key="item.teamId"
            :label="item.teamName"
            :value="item.teamId">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="计划开始时间" prop="startTime" label-width="16%" v-if="!dataForm.id">
      <el-date-picker
        v-model="dataForm.startTime"
        type="datetime"
        placeholder="开始时间"
        suffix-icon="el-icon-date"
        style="width: 200px">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="计划结束时间" prop="endTime" label-width="16%" v-if="!dataForm.id">
      <el-date-picker
        v-model="dataForm.endTime"
        type="datetime"
        placeholder="结束时间"
        suffix-icon="el-icon-date"
        style="width: 200px">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="项目状态" prop="statusId">
      <template>
        <el-radio-group v-model="dataForm.statusId">
          <el-radio :label="0">未开始</el-radio>
          <el-radio :label="1">开发中</el-radio>
          <el-radio :label="2">运维中</el-radio>
          <el-radio :label="3">暂停中</el-radio>
          <el-radio :label="4">已结束</el-radio>
        </el-radio-group>
      </template>
    </el-form-item>
      <el-form-item label="项目评分" v-if="isScore()">
        <el-rate
          v-model="projectScore"
          :colors="colors"
          prop="projectScore">
        </el-rate>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        collegeList: [],
        tutorList: [],
        teamList: [],
        collegeListTreeProps: {
          label: 'name',
          children: 'children'
        },
        projectScore: null,
        colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
        dataForm: {
          id: 0,
          projectName: '',
          projectDes: '',
          collegeId: '',
          tutorId: '',
          teamId: '',
          startTime: '',
          endTime: '',
          statusId: ''
        },
        dataRule: {
          projectName: [
            { required: true, message: '项目名不能为空', trigger: 'blur' }
          ],
          projectDes: [
            { required: true, message: '项目描述不能为空', trigger: 'blur' }
          ],
          collegeId: [
            { required: true, message: '项目所属学院编码不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '项目计划开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '项目计划结束时间不能为空', trigger: 'blur' }
          ],
          statusId: [
            { required: true, message: '项目状态编码不能为空', trigger: 'blur' }
          ],
          projectScore: [
            { required: true, message: '完结项目分数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/business/tutor/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.tutorList = data && data.code === 0 ? data.tutorList : []
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/sys/college/list'),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.collegeList = treeDataTranslate(data, 'collegeId')
            }).then(() => {
              this.visible = true
              this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                this.$refs.collegeListTree.setCheckedKeys([])
              })
            })
          }
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/team/select'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.teamList = data && data.code === 0 ? data.teamList : []
          }).then(() => {
            this.visible = true
            this.$nextTick(() => {
              this.$refs['dataForm'].resetFields()
              this.$refs.collegeListTree.setCheckedKeys([])
            })
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/project/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.projectName = data.busProject[0].projectName,
                this.dataForm.tutorId = data.busProject[0].userName,
                this.dataForm.teamId = data.busProject[0].teamName,
                this.dataForm.statusId = data.busProject[0].projectName
              }
            })
          }
        })
      },
      handleCheckChange(data, checked, indeterminate) {
        this.dataForm.collegeId = data.collegeId
      },
      isScore() {
        if (this.dataForm.statusId === 4) {
          return true
        }else {
          return false
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/project/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectName': this.dataForm.projectName,
                'projectDescription': this.dataForm.projectDes,
                'collegeId': this.dataForm.collegeId,
                'projectTutor': this.dataForm.tutorId,
                'teamId': this.dataForm.teamId,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'statusId': this.dataForm.statusId,
                'projectScore': this.projectScore
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
